package com.cloud.pole.user.service.collection.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.user.biz.BaseUserBiz;
import com.cloud.pole.user.biz.collection.UserCollectionBiz;
import com.cloud.pole.user.biz.collection.UserCollectionTypeBiz;
import com.cloud.pole.user.model.entity.collection.UserCollection;
import com.cloud.pole.user.model.entity.collection.UserCollectionType;
import com.cloud.pole.user.service.collection.UserCollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年2月21日
*cloud-user
*author:frank-chen
*/
@Service
@Slf4j
public class UserCollectionServiceImpl implements UserCollectionService {
	@Autowired
    private BaseUserBiz userBiz;
	
	@Autowired
    private UserCollectionBiz userCollectionBiz;
	
	@Autowired
    private UserCollectionTypeBiz userCollectionTypeBiz;
	
	@Override
	public void addCollection(Long userId, String userName, String objectId, String objectName, Integer objectTypeId, String note)
			throws Exception {
		// TODO Auto-generated method stub
		int status = 0;
		//判断typeId是否正确
		UserCollectionType userCollectionType = userCollectionTypeBiz.selectByTypeId(objectTypeId);
		if(userCollectionType == null || userCollectionType.getTypeStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "objectTypeId错误");
		}
		//判断是否已经添加过
		UserCollection tempUserCollection = userCollectionBiz.getUserCollectionByObjectId(userId, objectTypeId, objectId);
		if(tempUserCollection != null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "已经关注，不用重复关注");
		}
		UserCollection userCollection = new UserCollection();
		userCollection.setCreateTime(new Date());
		userCollection.setObjectId(objectId);
		userCollection.setObjectName(objectName);
		userCollection.setObjectNote(note);
		userCollection.setObjectTypeId(objectTypeId);
		userCollection.setUserId(userId);
		userCollection.setUserName(userName);
		userCollectionBiz.insertSelective(userCollection);
		return;
	}

	@Override
	public List<UserCollection> getUserCollectionList(Long userId, Integer objectTypeId) throws Exception {
		// TODO Auto-generated method stub
		//判断typeId是否正确
		UserCollectionType userCollectionType = userCollectionTypeBiz.selectByTypeId(objectTypeId);
		if (userCollectionType == null || userCollectionType.getTypeStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "objectTypeId错误");
		}	
		return userCollectionBiz.getUserCollectionList(userId, objectTypeId);
	}

	@Override
	public UserCollection getUserCollection(Long userId, Integer objectTypeId, String objectId) throws Exception {
		// TODO Auto-generated method stub
		//判断typeId是否正确
		UserCollectionType userCollectionType = userCollectionTypeBiz.selectByTypeId(objectTypeId);
		if (userCollectionType == null || userCollectionType.getTypeStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "objectTypeId错误");
		}	
		return userCollectionBiz.getUserCollectionByObjectId(userId, objectTypeId, objectId);
	}

	@Override
	public void cancelUserCollection(Long userId, Integer objectTypeId, String objectId) throws Exception {
		int status = 0;
		//判断typeId是否正确
		UserCollectionType userCollectionType = userCollectionTypeBiz.selectByTypeId(objectTypeId);
		if(userCollectionType == null || userCollectionType.getTypeStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "objectTypeId错误");
		}
		//判断是否已经添加过
		UserCollection tempUserCollection = userCollectionBiz.getUserCollectionByObjectId(userId, objectTypeId, objectId);
		if(tempUserCollection == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "未关注");
		}
		userCollectionBiz.deleteById(tempUserCollection);
		return;		
	}

	@Override
	public PageInfo<UserCollection> getPageCollectionList(Long userId, Integer objectTypeId, Integer pageNum,
			Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize); 
    	List<UserCollection> list = userCollectionBiz.getUserCollectionList(userId, objectTypeId);
    	PageInfo<UserCollection> page = new PageInfo<UserCollection>(list);
    	return page;
	}
}
